package apof.model
{
	import apof.util.Hasher;
	
	public class Transection extends ApofModel
	{
		public var tid : int=-1;
		public var amount : Number=0;
		public var name : String;
		public var time : Date=new Date();
		public var description : String="收支记录";
		public var shared : Boolean;
		public var aid : int=10;
		public var ctid : int=-1;
		public var offline : Boolean=true;
		
		public var account:Account=null;
		public var consumeType:ConsumeType=null;

		public function get stime():String { return time.toString();}
		public function set stime(s:String):void {
			time=new Date(Date.parse(s));
		}
		public function get dtime():String{
			return time.date+"/"+(time.month+1)+"/"+time.fullYear;
		}
		
		public function get absoluteTime():Number {return time.time;}
		
		public function get friendlyDate():String {
			var theDay:int=time.time/ MilisecondsOfADay - 0.5;
			var today:int=new Date().time/MilisecondsOfADay  - 0.5;
			if(theDay==today) return "Today";
			if(theDay==today-1) return "Yesterday";
			return dtime;
		}
		
		private static var MilisecondsOfADay:int=1000*60*60*24;
		public function get dateType():int {
			var theDay:int=time.time/ MilisecondsOfADay;
			return theDay % 4; 
		}
		
		public function get category():String {
			return consumeType?consumeType.name:"未分类";
		}
		public function get accountName():String{
			return account.name;
		}
		
		public function get bShared():int {
			return shared?1:0;
		}
		public function set bShared(b:int):void {
			shared=(b==1);
		}
		public function get bOffline():int {
			return offline?1:0;
		}
		public function set bOffline(b:int) :void {
			offline=(b==1);
		}
		
		public function get hashCode():int {
			return Hasher.hashTansection(this);
		}

		//private static var init:Boolean = onInit();     
		public static function onInit():Boolean {
			var pt:Object=prototype;
			pt.classname="Transection";		
			pt.fields=["tid","amount","name","stime","description","bShared","aid","ctid","bOffline"];
			pt.fieldTypes=["INT","NUMBER","VARCHAR(60)","VARCHAR(30)","TEXT","BIT","INT","INT","BIT"];
			pt.primaryKey="tid";
			pt.foreignKey={	"aid":	{"linkto":Account,	"aggregate":false,	"ref":"account","peerlink":"transections"},
							"ctid":	{"linkto":ConsumeType,	"aggregate":false,	"ref":"consumeType","peerlink":"transections"}
							};
			return true;
		}

		public function toString():String {
			var s:String="花了"+(-amount)+"元买了"+name+(description=="收支记录"?".":","+description);
			return s;
		}

		public override function get classInfo():Object {return prototype;}
		public static function get classInfo():Object {return prototype;}
	}
}